import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_bloc_demo/config/config_bloc.dart';

import 'login_page_cubit.dart';

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LoginPage();
  }
}

class _LoginPage extends State<LoginPage> {
  TextEditingController _controller;
  var colorList = [
    Colors.redAccent,
    Colors.orangeAccent,
    Colors.greenAccent,
    Colors.yellowAccent,
    Colors.blueAccent,
    Colors.purpleAccent,
    Colors.pinkAccent
  ];

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider<LoginPageCubit>(
      create: (BuildContext context) => LoginPageCubit(),
      child: BlocBuilder<LoginPageCubit, LoginPageState>(
        builder: (BuildContext context, LoginPageState state) => Scaffold(
          appBar: AppBar(
            title: Text("loginPage"),
          ),
          body: Column(
            children: [
              Container(
                margin: EdgeInsets.only(
                  top: 20,
                  left: 20,
                  right: 20,
                ),
                child: TextField(
                  controller: _controller,
                  inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                  style: TextStyle(color: Colors.blueAccent),
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: "用户id",
                    hintText: "请输入用户ID(数字)",
                  ),
                  onEditingComplete: () {},
                  onChanged: (v) {},
                  onSubmitted: (v) {},
                ),
              ),
              Visibility(
                child: Padding(
                  padding: EdgeInsets.all(20),
                  child: Text(
                      "姓名：${state?.name}  性别：${state?.sex} 年龄：${state?.age}"),
                ),
                visible: state != null,
              ),
              ElevatedButton(
                onPressed: () {
                  BlocProvider.of<LoginPageCubit>(context)
                      .login(context,int.parse(_controller.text.isNotEmpty ? _controller.text : "0"));
                },
                child: Text("登陆"),
              ),
              Container(child: Text("改变主题appBar样式"),margin: EdgeInsets.all(20),),
              Row(
                children: colorList
                    .map((e) => GestureDetector(
                          child: Container(
                            color: e,
                            width: 30,
                            height: 30,
                            padding: EdgeInsets.all(10),
                            margin: EdgeInsets.all(10),
                          ),
                          onTap: () {
                            BlocProvider.of<ConfigBloc>(context).add(AppBarEvent(e));
                          },
                        ))
                    .toList(),
              )
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}
